﻿using System;

namespace AIC_Notification
{
    class StudentStatusReportForLT: StatisticsReport
    {
        public StudentStatusReportForLT()
        {
            ReportTitle = "Student Status (Leadership)";
        }

        public override string InnerTableHeader()
        {
            string tableHeader = "";

            tableHeader += "<tr style=\"background-color: #483D8B; color: white;\">";
            tableHeader += "<th style=\"font-size: 14px;\"></th>";  // Leave 1st column empty
            tableHeader += "<th style=\"font-size: 14px;\">Total</th>";  // Add total column
            for (int i = 0; i < CityDB.NoOfCities; i++)
            {
                tableHeader += String.Format("<th style=\"font-size: 14px;\">{0}</th>", CityDB.GetCityName((CityDB.CityEnum)i));
            }
            tableHeader += "</tr>";

            return tableHeader;
        }

        public override string GetSubject()
        {
            return String.Format("{0} - {1:d}", ReportTitle, Global.Now());
        }

        private string InsertStudentCount()
        {
            string RowHTML = "";

            RowHTML += "<tr><td style=\"font-size: 14px;\"><u>Student Count</u></td>";

            // Insert Total Data
            RowHTML += String.Format("<td style=\"font-size: 14px;\">{0}</td>", Global.SOF_StudentDB.Students.Count);

            // Insert City Data
            foreach (var City in CityDB.Cities)
                RowHTML += String.Format("<td style=\"font-size: 14px;\">{0}</td>", City.Students.Count);

            RowHTML += "</tr>";

            return RowHTML;
        }

        private string InsertPendingDraftStatistics()
        {
            string RowHTML = "";
            string CityRowHTML = "";
            string TextColor = "";

            RowHTML += "<tr><td style=\"font-size: 14px;\"><u>Overdue Revision</u></td>";

            // Calculate City Data
            foreach (var City in CityDB.Cities)
            {
                if (City.ApplicationStats.TotalOverDuedRevision > 0)
                    TextColor = "background-color: #F8D3D3;";
                else
                    TextColor = "background-color: #ccffcc;";

                CityRowHTML += String.Format("<td style=\"font-size: 14px;{1}\">{0}</td>", City.ApplicationStats.TotalOverDuedRevision, TextColor);
            }

            // Insert Total Data
            {
                if (Statistics.ApplicationStats.TotalOverDuedRevision > 0)
                    TextColor = "background-color: #F8D3D3;";
                else
                    TextColor = "background-color: #ccffcc;";

                RowHTML += String.Format("<td style=\"font-size: 14px;{1}\">{0}</td>", Statistics.ApplicationStats.TotalOverDuedRevision, TextColor);
            }

            // Insert City Data
            RowHTML += CityRowHTML;

            RowHTML += "</tr>";

            return RowHTML;
        }

        private string InsertStudentStatusStatistics(int Rally, int SchoolNumber)
        {
            string RowHTML = "";
            string SchoolName = "";
            int FontSize = 14;

            if (Rally == 0 && SchoolNumber == 0)
                SchoolName = "CEF";
            else if (Rally == 1 && SchoolNumber == 0)
                SchoolName = "PS";
            else if (Rally == 1 && SchoolNumber == 1)
                SchoolName = "HAL";
            else
                SchoolName = String.Format("R{0}/School {1}", Rally, SchoolNumber+1);

            RowHTML += "<tr>";

            RowHTML += "<td style=\"padding: 2px 2px 2px 2px; text-align: center;\">";
            RowHTML += String.Format("<u>{0}</u>", SchoolName);
            RowHTML += "</td>";

            // Insert Total Data
            RowHTML += "<td style=\"padding: 2px 2px 2px 2px; text-align: center;\">";
            RowHTML += "<table style=\"margin-left:auto; margin-right: auto;\">";
            RowHTML += InsertCompletionStatistics(Statistics.RallyOfRallySchoolStats, Rally, SchoolNumber, FontSize, false);
            RowHTML += InsertEssayStatistics(Statistics.RallyOfRallySchoolStats, Rally, SchoolNumber, FontSize, false);
            RowHTML += "</table>";
            RowHTML += "</td>";

            // Insert City Data
            foreach (var City in CityDB.Cities)
            {
                RowHTML += "<td style=\"padding: 2px 2px 2px 2px; text-align: center;\">";
                RowHTML += "<table style=\"margin-left:auto; margin-right: auto;\">";
                RowHTML += InsertCompletionStatistics(City.RallyOfRallySchoolStats, Rally, SchoolNumber, FontSize, false);
                RowHTML += InsertEssayStatistics(City.RallyOfRallySchoolStats, Rally, SchoolNumber, FontSize, false);
                RowHTML += "</table>";
                RowHTML += "</td>";
            }

            RowHTML += "</tr>";

            return RowHTML;
        }

        public override string GenerateLedger()
        {
            string htmlLedger = "";

            htmlLedger += "<tr><td>";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\"><b><u>Student Essay Status</u></b>:<span style=\"color: red; font-size: 12px;\">*</span> = Student has overdue draft; <b>/R</b> = Has Revision; <b>/F</b> = Finalized</div> ";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\">";
            htmlLedger += "<b><u>Completion</u></b>: <b>NS</b> = Not Started; <b>IP</b> = In Progress; <b>CP</b> = Completed";
            htmlLedger += "</div>";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\">";
            htmlLedger += "<b><u>Essay</u></b>: <b>Avg</b>= Average # of Draft, <b>Min/Max</b> = Minimum and Maximum # of Draft";
            htmlLedger += "</div>";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\">";
            htmlLedger += "<b><u>Completion Calculation</u></b>: <b>CEF</b>= only CER is counted, not CEF; <b>HAL</b> = Only HAL is counted, not MAJ.";
            htmlLedger += "</div>";
            htmlLedger += "</td></tr>";

            return htmlLedger;
        }

        public override string GenerateInnerTable()
        {
            string InnerTable = "";

            InnerTable += InsertStudentCount();

            InnerTable += InsertPendingDraftStatistics();

            for (int i = 0; i <= RallyHelper.CurrentRally; i++)
            {
                int NoOfSchools = i != RallyHelper.NoOfRally ? 2 : SOF_Application.MAX_SCHOOL_IN_RALLY;
                for (int j = 0; j < NoOfSchools; j++)
                {
                    if (Statistics.RallyOfRallySchoolStats.Get(i,j) != null)
                        InnerTable += InsertStudentStatusStatistics(i, j);
                } // for
            } // for

            return InnerTable;
        }

        public override string EmptyInnerTable()
        {
            return "<tr><td style=\"padding: 10px 0 10px 0; text-align:center;\">No Student Status</td></tr>";
        }

        public override void SendReportAsEmail()
        {
            string HTMLBody;

            // Students = Global.SOF_Applications.Values.ToList<SOF_Application>();
            // RallySchoolStatsList = Statistics.RallySchoolStatsList;
            HeaderInformation = "Company";

            FileName = String.Format("{0}-{1}-{2:yyyyMMdd-HHmmss}.htm", ReportTitle, "Company", Global.Now());
            Global.SysUtil.Log("Generating Report: " + FileName);

            HTMLBody = GetHTMLBody();

            // Save Report to File
            SaveReportToFile(FileName, HTMLBody);

            // Send Report E-mail
            toRecipients.Clear();
            ccRecipients.Clear();

            toRecipients.Add("aic-lt@jq-edu.com");
            toRecipients.Add("academic.manager@jq-edu.com");
            ccRecipients.Add("wai.cheng@jq-edu.com");
            SubjectID = "Company";

            Global.SysUtil.SendHTML(toRecipients, ccRecipients, GetSubject(), HTMLBody);
        }
    }
}
